﻿

(function ($) {

    var methods = {
        init: function (options) {

            var settings = $.extend({
                'wrapper-class': 'file-wrapper',
                'button-text': 'Wybierz plik',
                'button-text-chosen': 'Zmień plik',
                'button-class': 'file-button'
            }, options);
            return this.each(function () {
                var $input = $(this);

                if ($input.data('pretty-input') == 1)
                    return;

                $input.data('pretty-input', 1);

                $input.css({
                    cursor: 'pointer',
                    height: '100%',
                    position: 'absolute',
                    top: '0',
                    right: '0',
                    fontSize: '100px'
                });
                $input.fadeTo(1, 0.01);

                var $fileWrapper = $input.wrap('<div />').parent();
                $fileWrapper.addClass(settings['wrapper-class']);
                $fileWrapper.css({
                    cursor: 'pointer',
                    display: 'inline-block',
                    overflow: 'hidden',
                    position: 'relative'
                });

                var $bnt = $('<span />');
                $bnt
                .appendTo($fileWrapper)
                .text(settings['button-text'])
                .addClass(settings['button-class'])
                .css({
                    cursor: 'pointer',
                    display: 'inline-block'
                });

                var $filetext = $('<span class="file-name" />');
                $filetext.appendTo($fileWrapper);

                $input
                .bind('change focus click', function () {
                    methods.update.apply($(this));
                });
                $input.data('bnt', $bnt);
                $input.data('filetext', $filetext);
                $input.data('settings', settings);

                methods.update.apply($(this)); // 

            });



        },

        update: function () {
            var $input = this;
            var $val = $input.val(),
                valArray = $val.split('\\'),
                newVal = valArray[valArray.length - 1],
                $bnt = $input.data('bnt'),
                $filetext = $input.data('filetext'),
                settings = $input.data('settings');

            if (newVal !== '') {
                $bnt.text(settings['button-text-chosen']);
                $filetext.text(newVal);
            } else {
                $bnt.text(settings['button-text']);
                $filetext.text('');
            }
        }
    };

    $.fn.prettyFileInput = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.tooltip');
        }
    };


})(jQuery);

